import datetime
import os.path
import sys

from selenium import webdriver
from selenium.webdriver.edge.service import Service
from webdriver_manager.microsoft import EdgeChromiumDriverManager


# 创建一个浏览器对象



class Driver:
    def __init__(self, options=None):
        # 初始化 Edge WebDriver
        if options is None:
            options = webdriver.EdgeOptions()

        # 创建 EdgeChromiumDriverManager 实例
        driver_manager = EdgeChromiumDriverManager()

        self.driver = webdriver.Edge(
            service=Service(driver_manager.install()),
            options=options
        )

    def getScreeShot(self):
        # 创建屏幕截图
        # 图片文件名称：2025-06-15-214556.png 根据每一天的日期文件夹来分类
        # 图片路径：../images/2025-06-15/2025-06-15-214556.png
        dirname = datetime.datetime.now().strftime("%Y-%m-%d")
        # 判断dirname文件夹是否已经存在，若不存在则创建文件夹
        if not os.path.exists("../images/"+dirname):
            os.mkdir("../images/"+dirname)

        filename = sys._getframe().f_back.f_code.co_name + datetime.datetime.now().strftime("%Y-%m-%d-%H%M%S")
        self.driver.save_screenshot("../images/"+dirname+"/"+filename+".png")

# 使用示例
BlogDriver = Driver()